//
//  CYXiangShuiTiWenViewController.m
//  cyydt
//
//  Created by chu on 2019/2/1.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYXiangShuiTiWenViewController.h"
#import "CYXiangShuiTiWenTableViewCell.h"

@interface CYXiangShuiTiWenViewController ()<UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
{
    UIButton *_rightItemBtn;
    NSInteger _selCount;
}
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSourceArr;

@end

@implementation CYXiangShuiTiWenViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"向谁提问";
    _selCount = 0;
//    [self.view addSubview:self.tableView];
    [self getData];
    self.tableView.estimatedRowHeight = 44;
    if (@available(iOS 11.0, *)) {
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    [self setRightItem];
}

- (void)setRightItem{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    _rightItemBtn = btn;
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn setTitleColor:RGBOF(0xF43636) forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [btn addTarget:self action:@selector(RighttouchEvent) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = item;
}

- (void)RighttouchEvent{
    NSMutableArray *muArr = [NSMutableArray arrayWithCapacity:0];
    for (CYFriendModel *model in self.dataSourceArr) {
        if ([model.isSelecte boolValue]) {
            [muArr addObject:model];
        }
    }
    if (_delegate && [_delegate respondsToSelector:@selector(doneFinishWithArr:)]) {
        [_delegate doneFinishWithArr:muArr];
    }
    
    [[AppDelegate sharedAppDelegate] popViewController];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CYXiangShuiTiWenTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CYXiangShuiTiWenTableViewCell"];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"CYXiangShuiTiWenTableViewCell" owner:nil options:nil][0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataSourceArr[indexPath.row];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self initSelValueWithIndexPath:indexPath];
    
}

- (void)initSelValueWithIndexPath:(NSIndexPath *)indexPath{
    CYFriendModel *model = self.dataSourceArr[indexPath.row];
    if ([model.isSelecte boolValue]) {
        model.isSelecte = [NSNumber numberWithBool:NO];
        _selCount -= 1;
        [self.tableView reloadData];
    }else{
        
        if (_selCount < 5) {
            for (CYFriendModel *model1 in self.dataSourceArr) {
                if (model1 == model) {
                    model.isSelecte = [NSNumber numberWithBool:YES];
                    _selCount += 1;
                }
            }
        }else{
            [self.view makeToast:@"最多选择5个" duration:1.5 position:CSToastPositionCenter];
        }
        [self.tableView reloadData];
    }
    [_rightItemBtn setTitle:[NSString stringWithFormat:@"确定%ld/5",_selCount] forState:UIControlStateNormal];
}

- (void)getData{
    self.page = 1;
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"ask/experts"];
    
    NSDictionary *param = @{@"pageNo":@1, @"pageSize":@"10"};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"专家列表 ---- %@",responseResult);
        [self.tableView removeFromSuperview];
        [self.view addSubview:self.tableView];
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if (responseResult[@"data"]) {
                    NSDictionary *data = responseResult[@"data"];
                    NSArray *list = data[@"users"];
                    [self.dataSourceArr removeAllObjects];
                    for (NSDictionary *dic in list) {
                        NSMutableDictionary *muDic = [NSMutableDictionary dictionaryWithDictionary:dic];
                        [muDic setObject:[NSNumber numberWithBool:NO] forKey:@"isSelecte"];
                        CYFriendModel *model = [CYFriendModel mj_objectWithKeyValues:muDic];
                        
                        [self.dataSourceArr addObject:model];
                    }
                    
                }
            }else{
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
        [self.tableView reloadData];

    }];
}

- (void)getMoreData{
    self.page ++;
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"getMyComments"];
    NSDictionary *param = @{@"pageNo":[NSNumber numberWithInteger:self.page], @"pageSize":@"10"};
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:param ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"专家列表 ---- %@",responseResult);
        if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
        if ([responseResult objectForKey:@"resError"]) {
            [self.view makeToast:ResErrorMessage];
            self.page -- ;
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                if (responseResult[@"data"]) {
                    NSDictionary *data = responseResult[@"data"];
                    NSArray *list = data[@"users"];
                    for (NSDictionary *dic in list) {
                        NSMutableDictionary *muDic = [NSMutableDictionary dictionaryWithDictionary:dic];
                        [muDic setObject:[NSNumber numberWithBool:NO] forKey:@"isSelecte"];
                        CYFriendModel *model = [CYFriendModel mj_objectWithKeyValues:muDic];
                        [self.dataSourceArr addObject:model];
                    }
                    [self.tableView reloadData];
                }
            }else{
                self.page -- ;
                [self.view makeToast:responseResult[Response_Message] duration:1.5 position:CSToastPositionCenter];
            }
        }
    }];
}

#pragma mark - DZNEmptyDataSetDelegate
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    return [UIImage imageNamed:@"wuneir"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"当前页面没有内容！";
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:16],NSForegroundColorAttributeName:RGBOF(0x999999)
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    // 空白页面被点击时开启动画，reloadEmptyDataSet
    [self getData];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, kWindowH - Height_NavBar) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = BackGroundColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self getData];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self getMoreData];
        }];
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        
        [_tableView registerNib:[UINib nibWithNibName:@"CYXiangShuiTiWenTableViewCell" bundle:nil] forCellReuseIdentifier:@"CYXiangShuiTiWenTableViewCell"];
    }
    return _tableView;
}

- (NSMutableArray *)dataSourceArr{
    if (!_dataSourceArr) {
        _dataSourceArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArr;
}

@end
